﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.Admin.SlideShow
{
    public partial class Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Utils.CheckAuthentication())
            {
                string ID = Request.QueryString["ID"] ?? String.Empty;
                if (ID == String.Empty)
                {
                    Response.Redirect("~/Admin/SlideShow/Index.aspx");
                }
                else
                {
                    if (!IsPostBack)
                    {
                        LoadData();
                    }
                }
            }
        }
        private void LoadData()
        {
            info.InnerHtml = string.Empty;
            if ((string)Session["Info"] != "")
            {
                info.InnerHtml = (string)Session["Info"];
                Session["Info"] = "";
            }
            long ID = long.Parse(Request.QueryString["ID"]);
            SLIDESHOW slide = SlideShowModels.GetByID(ID);
            if (slide != null)
            {
                txtLink.Value = slide.Link;
                txtOrdinal.Value = slide.ThuTu.ToString();
                txtSlideName.Value = slide.TieuDe;
                txtSummary.Value = slide.MoTa;
                imgSlide.ImageUrl = ResolveClientUrl("~/images/slides/default.png");
                if (slide.HinhAnh != null)
                {
                    imgSlide.ImageUrl = ResolveClientUrl("~/images/slides/" + slide.HinhAnh);
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtSlideName.Value != "")
            {                
                long ID = long.Parse(Request.QueryString["ID"]);
                SLIDESHOW slide = SlideShowModels.GetByID(ID);
                if (fUpload.HasFile)
                {
                    string fileName = Utils.UploadImage(fUpload.PostedFile, "~/images/slides/");
                    if (fileName == String.Empty)
                    {
                        Session["Info"] = "<div class='alert alert-danger'>Vui lòng chọn file đúng định dạng !!!</div>";
                        Response.Redirect("~/Admin/SlideShow/Update.aspx?ID=" + Request.QueryString["ID"]);
                    }
                    else
                    {
                        //xoa file cu
                        Utils.DeleteFile(ResolveClientUrl("~/images/slides/" + slide.HinhAnh));
                        slide.HinhAnh = fileName;
                    }                    
                }                

                slide.Link = txtLink.Value.Replace("http://", "").Replace("https://", "").TrimEnd('/');
                slide.MoTa = txtSummary.Value;
                slide.ThuTu = int.Parse(txtOrdinal.Value);
                slide.TieuDe = txtSlideName.Value;                
                bool status = SlideShowModels.Update(slide);
                if (status == true)
                {
                    Session["Info"] = "<div class='alert alert-success'>Cập nhật slide thành công !!!</div>";
                    Response.Redirect("~/Admin/SlideShow/Index.aspx");
                }
                else
                {
                    info.InnerHtml = "<div class='alert alert-danger'>Đã xảy ra lỗi, vui lòng thử lại !!!</div>";
                }
            }
            else
            {
                info.InnerHtml = "<div class='alert alert-danger'>Vui lòng nhập đủ thông tin slide !!!</div>";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/SlideShow/Index.aspx");
        }
    }
}